package com.yangxk.kadmin.auth.config;

import com.yangxk.kadmin.auth.beans.TableBean;
import com.yangxk.kadmin.auth.beans.WhiteListBean;
import com.yangxk.kadmin.auth.dao.DbDao;
import com.yangxk.kadmin.auth.filter.AuthFilter;
import com.yangxk.kadmin.auth.intercepter.AuthIntercepter;
import com.yangxk.kadmin.auth.service.CoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import java.awt.image.DataBufferDouble;

/**
 * 权限自动配置类
 *
 * @author yangxk
 * @date 2018/9/9 14:10
 */
@ConditionalOnWebApplication
@ConditionalOnBean(AuthConfigure.class)
@Configuration
public class AuthAutoConfiguration implements WebMvcConfigurer{

    @Autowired
    private AuthConfigure authConfigure;

    @Bean
    public TableBean tableBean() {
        TableBean tableBean = new TableBean();
        authConfigure.setTableInfo(tableBean);
        return tableBean;
    }

    @Bean
    public WhiteListBean whiteListBean() {
        WhiteListBean whiteListBean = new WhiteListBean();
        authConfigure.setWhiteList(whiteListBean);
        return whiteListBean;
    }

//    @Bean
//    public FilterRegistrationBean authFilterRegistration() {
//        FilterRegistrationBean<AuthFilter> bean = new FilterRegistrationBean<>();
//        bean.setFilter(authFilter());
//        bean.addUrlPatterns("/*");
//        bean.setOrder(-1);
//        return bean;
//    }
//
//    @Bean
//    public AuthFilter authFilter() {
//        return new AuthFilter();
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authIntercepter()).addPathPatterns("/**").order(-1);
    }

    @Bean
    public AuthIntercepter authIntercepter() {
        return new AuthIntercepter();
    }

    @Bean
    public CoreService coreService() {
        return new CoreService();
    }

    @Bean
    public DbDao dbDao() {
        return new DbDao();
    }

}
